package actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import beans.User;
import beans.UserDAO;

public class UserActionFindAll implements UserAction {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//1模拟得到数据
		String[] usernos = { "1101", "1102", "1103" };
		String[] userpwds = { "123", "124", "125" };
		String[] usernames = { "张三", "李四", "王五" };
		String[] usersexs = { "男", "男", "女" };
		String[] userinterests = { "足球-篮球", "足球-电影", "电影-小说" };

		List<Map<String, Object>> list = new ArrayList<>();

		for (int i = 0; i < usernos.length; i++) {
			Map<String, Object> map2 = new HashMap<>();
			map2.put("userno", usernos[i]);
			map2.put("userpwd", userpwds[i]);
			map2.put("username", usernames[i]);
			map2.put("usersex", usersexs[i]);
			map2.put("userinterest", userinterests[i]);
			list.add(map2);
		}
		
		
		//下面为jstl方式显示做准备
		String test="bb";
		request.getSession().setAttribute("test", test);//由原来的pageContext改为request
		request.getSession().setAttribute("list", list);
		
		//如果list等转成json对象，使用jstl也是可以的，所以，使用ajax，使用服务端的json也是可以的，原来都是使用客户端的json
		
		String jsonstr=JSON.toJSONString(list);
		JSONArray jsonarray=JSONArray.parseArray(jsonstr);
		request.getSession().setAttribute("list2", jsonarray);
		
		//从下面是正式从数据库中取出的数据，放在div4中
		List<User> list4= null;//这是第5单元，使用到user
		UserDAO userDAO=new UserDAO();
		list4=userDAO.findall2();//注意，这里换成了findall2
		request.getSession().setAttribute("list4", list4);
		
		response.sendRedirect("findall.jsp");
		

	}

}
